'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;
  const sys_user = app.model.define('sys_user', {
    userId: {
      type: INTEGER(20),
      comment: "用户ID",
      allowNull:false,
      primaryKey: true,
      autoIncrement: true,
      field:"user_id"
    },
    deptId:{
      type:INTEGER(20),
      comment:"部门ID",
      field:"dept_id"
    },
    userName:{
      type:STRING(30),
      comment:"登录账号",
      allowNull:false,
      field:"user_name"
    },
    nickName:{
      type:STRING(30),
      comment:"用户昵称",
      defaultValue:"",
      field:"nick_name"
    },
    userType:{
      type:STRING(2),
      comment:"用户类型",
      defaultValue:"00",
      field:"user_type"
    },
    email:{
      type:STRING(50),
      comment:"用户邮箱",
      defaultValue:""
    },
    phonenumber:{
      type:STRING(11),
      comment:"手机号码",
      defaultValue:""
    },
    sex:{
      type:STRING(1),
      comment:"用户性别（0男 1女 2未知）",
      defaultValue:""
    },
    avatar:{
      type:STRING(100),
      comment:"头像路径",
      defaultValue:""
    },
    password:{
      type:STRING(100),
      comment:"密码",
      defaultValue:""
    },
    status:{
      type:STRING(1),
      comment:"帐号状态（0正常 1停用）",
      defaultValue:"0"
    },
    loginIp:{
      type:STRING(128),
      comment:"最后登录IP",
      defaultValue:"",
      field:"login_ip"
    },
    loginDate:{
      type:DATE,
      comment:"最后登录时间",
      field:"login_date"
    },
    createBy:{
      type:STRING(64),
      comment:"创建者",
      field:"create_by"
    },
    updateBy:{
      type:STRING(64),
      update_by:"更新者",
      field:"update_by"
    },
    createTime:{
      type:DATE,
      field:"created_at"
    },
    remark:{
      type:STRING(500),
      defaultValue:"",
      comment:"备注"
    }
  }, {
    paranoid: true,
    freezeTableName: true
  });

  // 关联sys_role
  sys_user.associate = ()=>{
    app.model.SysUser.belongsToMany(app.model.SysRole,{
      through:app.model.SysUserRole
    });
    app.model.SysUser.belongsToMany(app.model.SysPost,{
      through:app.model.SysUserPost
    });
    app.model.SysUser.belongsTo(app.model.SysDept,{
      foreignKey:{
        name:"dept_id",
        allowNull:true
      }
    })
  };



  return sys_user;
};